package com.zhx.sweet.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.zhx.sweet.util.DozerMapperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;
import java.util.List;

/**
 * BaseController
 */
@Slf4j
public class BaseController<T extends Serializable, ID extends Serializable> {

    @Autowired
    private IService<T> mModelService;

    @Autowired
    private DozerMapperUtils mDozerMapperUtils;

    public IService<T> getModelService() {
        return mModelService;
    }

    public void setModelService(IService<T> mModelService) {
        this.mModelService = mModelService;
    }

    /**
     * 返回responseEntity对象
     * @param code HTTP状态码
     * @param data 数据
     * @return ResponseEntity的实例
     */
    public ResponseEntity buildResponseEntity(int code, Object data) {
        return ResponseEntity.status(code).body(data);
    }

    /**
     * 返回成功
     * @param data 数据
     * @return ResponseEntity的实例
     */
    public ResponseEntity buildSuccessResponseEntity(Object data) {
        return buildResponseEntity(HttpStatus.OK.value(), data);
    }

    public ResponseEntity buildSuccessResponseEntity(Object data, String msg) {
        return buildResponseEntity(HttpStatus.OK.value(), data);
    }

    /**
     * 返回成功
     * @return ResponseEntity的实例
     */
    public ResponseEntity buildSuccessResponseEntity() {
        return buildSuccessResponseEntity("");
    }

    /**
     * 返回失败
     * @param msg 错误信息
     * @return ResponseEntity的实例
     */
    public ResponseEntity buildFailedResponseEntity(String msg) {
        return buildResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), "");
    }

    /**
     * 返回失败
     * @return ResponseEntity的实例
     */
    public ResponseEntity buildFailedResponseEntity() {
        return buildFailedResponseEntity("");
    }

    /**
     * 转VO
     * @param obj 原对象
     * @param tsClass VO的类名 (XXX.class)
     * @param <VO> VO的类型 (XXX)
     * @return
     */
    public <VO> VO mapperVO(Object obj, Class<VO> tsClass) {
        return mDozerMapperUtils.map(obj, tsClass);
    }

    /**
     * 转listVO
     *
     * @param poList 原list对象
     * @param voClass listVO的类名 (XXX.class)
     * @param <PO> 原对象类型
     * @param <VO> listVO的类型 (XXX)
     * @return
     */
    public <PO, VO> List<VO> mapperListVO(List<PO> poList, Class<VO> voClass) {
        return mDozerMapperUtils.mapList(poList, voClass);
    }

    /**
     * 转pageVO
     *
     * @param page 原page对象
     * @param voClass pageVO的类名（XXX.class）
     * @param <PO> 原page对象类型
     * @param <VO> pageVO的类型 (XXX)
     * @return
     */
    public <PO, VO> Page<VO> mapperPageVO(Page<PO> page, Class<VO> voClass) {
        return mDozerMapperUtils.mapperPage(page, voClass);
    }

}
